#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

int q[4];
bool visited[4];
int hand;

void find(int x){
    if(x == 0){
        return;
    }

    visited[x] = true;
    find(q[x]);
}

bool check(){
    for (int i = 1; i <= 3; i ++){
        if(!visited[i]){
            return false;
        }
    }
    return true;
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T;
    cin >> T;
    while(T--){
        memset(visited, 0, sizeof visited);
        cin >> hand;
        for (int i = 1; i <= 3; i ++){
            cin >> q[i];
        }
        find(hand);

        if(check()){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}
